package com.huatai.exchange.receive;

import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.Constants;
import com.faneui.common.utils.ListUtils;
import com.faneui.common.utils.StringUtil;
import com.faneui.integration.smb.SmbFileUtils;
import com.google.common.collect.Lists;
import com.huatai.exchange.facade.FileSourceConstants;
import com.huatai.exchange.utils.VelocityUtils;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import org.apache.commons.lang3.StringUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;
import java.util.Map;

@Service("fileDataSmbReceiveService")
public class FileDataSmbReceiveServiceImpl extends AbstractFileDataReceiveService {

    @Override
    public List<String> findAndDownloadFileList(String srcPath, String srcName, String markName, String fileSuffix,String tmpDirPath, Map<String, Object> resvMap) throws BusinessException {
        NtlmPasswordAuthentication auth = getAuth(resvMap);
        List<String> fileNames = Lists.newArrayList();
        try {
            SmbFile smbDir = new SmbFile(srcPath, auth);
            if (!smbDir.exists()) {
                throw new BusinessException("数据未到");
            }
            //  smbFileNames 是 文件名称
            List<String> smbFileNames = Lists.newArrayList(smbDir.list());
            // logger.info("开始处理");
            // 校验标志文件是否到
            checkFileAvailableMark(markName, smbFileNames, resvMap); // 标志文件 是否

            for (String smbFileName : smbFileNames) {
                if (!StringUtil.wildcardMatch(smbFileName, srcName)) {
                    continue;
                }
                SmbFileUtils.copyLocalFileFromRemote(new File(tmpDirPath + smbFileName), srcPath + smbFileName, auth);
                fileNames.add(smbFileName) ;
            }
        } catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
        return fileNames;
    }


    public void checkFileAvailableMark(String markName, List<String> smbFileNames, Map<String, Object> resvMap) throws BusinessException {
        if (ListUtils.isEmpty(smbFileNames)) {
            throw new BusinessException("标志文件未到");
        }
        // 兼顾两种模式校验,自动判断
        // 1,velocity 校验
        // 2,标志文件
        if(markName.contains("#set")) {
            Velocity.init();
            VelocityContext context = velocityHelper.fillContext(resvMap);
            try {
                String checkResult = VelocityUtils.evaluate(context, markName);
                // 交换校验指标 需要在这里标识
                if (!Constants.boolean_true.equals(checkResult)) {
                    throw new BusinessException(checkResult);
                }
            } catch (Exception ex) {
                BusinessException.throwBusinessException(ex);
            }
        }else  {
            String[] markNames = markName.split(",");
            for (String name : markNames) {
                boolean b = false;
                for (String filePath : smbFileNames) {
                    logger.info("filePath:" + filePath);
                    if (filePath.contains(name)) {
                        b = true;
                        break;
                    }
                    if (name.contains("*") && StringUtil.wildcardMatch(filePath, name)) {
                        b = true;
                        break;
                    }
                }
                if (!b) {
                    throw new BusinessException("标志文件[" + name + "]未到");
                }
            }
        }
    }

    private NtlmPasswordAuthentication getAuth(Map<String, Object> resvMap ) throws BusinessException{
        String url = (String)resvMap.get(FileSourceConstants.SMB_URL) ;
        if(StringUtils.isBlank(url))
            return SmbFileUtils.createAuthHdisk() ;
        String userName = (String)resvMap.get(FileSourceConstants.SMB_USER_NAME) ;
        String password = (String)resvMap.get(FileSourceConstants.SMB_PASSWORD) ;
        return SmbFileUtils.createAuth(url,userName,password);
    }
}
